# coding=utf-8
# author:xc
# datetime:2018/10/16 15:55

from dbConnection.MysqlConn import Mysql
from app import util, upload_files
from flask import request, render_template
from . import api


@api.route('/api/course/detail', methods=['POST', 'GET'])
def api_course_detail():
    try:
        cid = request.values.get('cid')
        page = request.values.get('page', default=1)
        page = int(page)
        num = (page - 1) * util.PAGE_NUM
        mysql = Mysql()
        sql = "select * from girl_course_chapter where cid='%s' limit %d,%d" % (cid, num, util.PAGE_NUM)
        res = mysql.getAll(sql, None)
        mysql.dispose()
        detail = []
        code = util.CODE_EMPTY
        if res:
            code = util.CODE_SUCCESS
            for x in res:
                data = dict()
                data['id'] = x['id']
                data['chapterName'] = x['chapterName']
                if not x['url'].startswith('http'):
                    data['url'] = upload_files.baseUrl + x['url']
                else:
                    data['url'] = x['url']
                data['flag'] = x['flag']
                data['clickNum'] = x['clickNum']
                detail.append(data)
    except Exception as e:
        print e
        code = util.CODE_ERROR
        detail = []
    data = {
        'code': code,
        'data': detail
    }
    return util.to_json(data)
